package com.shelpe.services.sustainer.client;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import com.shelpe.services.sustainer.client.AutoProductQueueClient.AutoProductQueueClientFallback;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@FeignClient(name="${feign.algorithmserver.name}", url="${feign.algorithmserver.url}", fallback = AutoProductQueueClientFallback.class)
public interface AutoProductQueueClient {

	@RequestMapping(path = "/algorithmservice/users/{user_id}/autoProductQueues/@start", method = RequestMethod.GET,  consumes="application/json;charset=UTF-8")
	AdgroupDto startAutoDrive(@PathVariable("user_id") long userID, @RequestParam("campaign_id") long campaignID,
			@RequestParam("product_id") long productID);
	
	@Data
	public static class AutoDrivingDto{
		@JsonProperty(value = "success", access = Access.READ_ONLY)
		private boolean success;

		@JsonProperty("campaign_id")
		private long campaignID;
		@JsonProperty("product_id")
		private long productID;
		@JsonProperty("default_price")
		private int defaultPrice;
		@JsonProperty("adgroup_id")
		private long adgroupID;
	}
	
	@RequestMapping(path = "/algorithmservice/users/{user_id}/autoProductQueues/@word", method = RequestMethod.GET,  consumes="application/json;charset=UTF-8")
	AutoDrivingDto startChooseWords(@PathVariable("user_id") long userID,@RequestParam("campaign_id") long campaignID,
			@RequestParam("product_id") long productID);

	@RequestMapping(path = "/algorithmservice/users/{user_id}/autoProductQueues/@adjust", method = RequestMethod.GET,  consumes="application/json;charset=UTF-8")
	AutoDrivingDto adjustAdgroup(@PathVariable("user_id") long userID,@RequestParam("adgroup_id") long adgroupID);
	
	@Data
	public static class AdgroupDto {

		@JsonProperty("adgroup_id")
		private long adgroupID;
		@JsonProperty("user_id")
		private long userID;
		@JsonProperty("campaign_id")
		private long campaignID;
		@JsonProperty("product_id")
		private long productID;
	}
	
	
	
	@RequestMapping(path = "/algorithmservice/users/{user_id}/mobileQscores/@execute", method = RequestMethod.GET,  consumes="application/json;charset=UTF-8")
	ExecutionRes executeMobileQscore(@RequestParam("user_id") long userID, @RequestParam("adgroup_id") long adgroupID);
	
	@Data
	public static class ExecutionRes{
		private boolean success;

		@JsonProperty("campaign_id")
		private long campaignID;
		@JsonProperty("product_id")
		private long productID;
		@JsonProperty("default_price")
		private int defaultPrice;
		@JsonProperty("adgroup_id")
		private long adgroupID;
	}
	
	@Service
	@Slf4j
	public static class AutoProductQueueClientFallback implements AutoProductQueueClient{

		@Override
		public AdgroupDto startAutoDrive(long userID,long campaignID, long productID) {
			log.error("to startAutoDrive fallback: {}, {}", campaignID, productID);
			return new AdgroupDto();
		}

		@Override
		public AutoDrivingDto startChooseWords(long userID, long campaignID, long productID) {
			log.error("to startChooseWords fallback: {}, {}", campaignID, productID);
			return new AutoDrivingDto();
		}

		@Override
		public AutoDrivingDto adjustAdgroup(long userID, long adgroupID) {
			log.error("to adjustAdgroup fallback: {}", adgroupID);
			return new AutoDrivingDto();
		}

		@Override
		public ExecutionRes executeMobileQscore(long userID, long adgroupID) {
			log.error("to executeMobileQscore fallback: {}", adgroupID);
			return new ExecutionRes();
		}
		
	}
}